package com.corner.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.corner.R;
import com.corner.model.cornerservice.domain.Location;
import com.corner.presenter.imp.LocationListItenPresenter;
import com.corner.ui.activity.ProfileActivity;
import com.corner.ui.base.BaseMVPAdapter;
import com.corner.ui.view.ILocationListItemView;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by Administrator on 2015/7/23，10.49.
 * 描述：
 */
public class LocationListAdapter extends BaseMVPAdapter<LocationListItenPresenter> implements ILocationListItemView, View.OnClickListener {

    private Context context;
    private List<Location> locations;

    public LocationListAdapter(Context context, List<Location> locations) {
        this.context = context;
        this.locations = locations;
    }

    @Override
    protected LocationListItenPresenter createP() {
        return new LocationListItenPresenter();
    }

    @Override
    public int getCount() {
        return locations.size();
    }

    @Override
    public Object getItem(int position) {
        return locations.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lv_city_list_item, null);
            Holder holder = new Holder();
            InjectViewUtils.injectView(holder, convertView, context);
            convertView.setTag(holder);
        }
        Holder holder = (Holder) convertView.getTag();
        Location location = locations.get(position);
        holder.tvLocationName.setText(location.getLocationName());
        holder.location = location;
        if (location.contentNum != null) {
            String contentNum = context.getString(R.string.location_has_create_num)
                    .replace(context.getString(R.string.location_has_create_num_xx), location.contentNum.toString());
            holder.tvContentsNum.setText(contentNum);
        } else {
            if (!location.hasRequestContentNum) {
                presenter.requestContentNum(location, holder);
                location.hasRequestContentNum = true;
            }
        }

        if (location.user != null) {
            if (location.user.getAvaterUrl() != null) {
                holder.ivAvater.setImageURI(Uri.parse(location.user.getAvaterUrl()));
            } else {
                holder.ivAvater.setImageURI(null);
            }
            holder.ivAvater.setOnClickListener(this);
            holder.ivAvater.setTag(location);
        } else {
            if(!location.hasRequestUser) {
                holder.ivAvater.setImageURI(null);
                presenter.requestAvater(location, holder);
                location.hasRequestUser = true;
            }
        }
        return convertView;
    }


    @Override
    public void setContentsNum(Location s, Holder holder) {
        if (holder.location != null && s.getObjectId().equals(holder.location.getObjectId())) {
            if (s.contentNum != 0) {
                String contentNum = context.getString(R.string.location_has_create_num)
                        .replace(context.getString(R.string.location_has_create_num_xx), s.contentNum.toString());
                holder.tvContentsNum.setText(contentNum);
            } else {
//                holder.tvContentsNum.setText(null);
                locations.remove(s);
                notifyDataSetChanged();
            }
        }
    }

    @Override
    public void setUser(Location location, Holder holder) {
        if (holder.location != null && location.getObjectId().equals(holder.location.getObjectId())) {
            if (location.user != null) {
                if (location.user.getAvaterUrl() != null) {
                    holder.ivAvater.setImageURI(Uri.parse(location.user.getAvaterUrl()));
                } else {
                    holder.ivAvater.setImageURI(null);
                }
                holder.ivAvater.setOnClickListener(this);
                holder.ivAvater.setTag(location);
            } else {
                holder.ivAvater.setImageURI(null);
            }
        }
    }

    public void changeList(List<Location> locations) {
        this.locations = locations;
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (v.getTag() != null) {
            Location location = (Location) v.getTag();
            if (location.user != null) {
                Intent intent = new Intent(context, ProfileActivity.class);
                intent.putExtra(ProfileActivity.USER_ID, location.user.getObjectId());
                context.startActivity(intent);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locations = null;
        context = null;
    }

    public class Holder {
        @FindView(R.id.iv_frag_location_list_avater)
        SimpleDraweeView ivAvater;
        @FindView(R.id.tv_frag_location_list_name)
        TextView tvLocationName;
        @FindView(R.id.tv_frag_location_list_contents_num)
        TextView tvContentsNum;
        Location location;
    }
}
